using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Decav.Gatsb.Configuration;
using Decav.Gatsb.Data;
using Decav.Reality;

namespace Decav.Gatsb.Application.CommandWorkflow
{
    /// <summary>
    /// Sets a users current location and notifies that user.
    /// </summary>
	public partial class SetCurrentLocationActivity: SequenceActivity
	{
		public SetCurrentLocationActivity()
		{
			InitializeComponent();
		}

        /// <summary>
        /// The dependency property that gets or sets the location to set for the user.
        /// </summary>
        public static DependencyProperty LocationProperty = DependencyProperty.Register(
            "Location", typeof(Location), typeof(SetCurrentLocationActivity));

        /// <summary>
        /// Gets or sets the location to set for the user.
        /// </summary>
        public Location Location
        {
            get { return (Location)GetValue(LocationProperty); }
            set { SetValue(LocationProperty, value); }
        }

        /// <summary>
        /// Gets the address notification messages are sent from.
        /// </summary>
        public string NotificationSenderAddress
        {
            get
            {
                if (GatsbConfigurationSection.Current == null)
                    return "m@gatsb.com";

                return GatsbConfigurationSection.Current.NotificationFromAddress;
            }
        }

        /// <summary>
        /// Gets the message sent to the user once the posting is complete.
        /// </summary>
        public string OutgoingMessage
        {
            get
            {
                if (Location == null)
                    return null;

                if (Location.Temporary)
                {
                    return string.Format("Okay, you're at {0}.\r\n\r\nSend a picture or txt message to post to this location!",
                        Location.Name);
                }
                else
                {
                    return string.Format("Okay, you're at {0} ({1}).\r\n\r\nSend a picture or txt message to post to this location!",
                        Location.Name, Location.Address.Address1);
                }
            }
        }

        private void setLocation_ExecuteCode(object sender, EventArgs e)
        {
            GatsbApplication.Current.SetUserLocation(Location.Id);
        }

        private void notifyNearbyUsers_ExecuteCode(object sender, EventArgs e)
        {
            User[] nearbyUsers = null;
            using (IGatsbDataAdapter adapt = GatsbApplication.Current.GetDataAdapter())
                nearbyUsers = adapt.GetLocationCurrentVisitors(Location.Id);

            string body = string.Format("{0} just checked in to {1}.", GatsbApplication.Current.CurrentUser.Name,
                Location.Name);
            
            foreach (User nearbyUser in nearbyUsers)
            {
                // don't send to yourself
                if (nearbyUser.Id == GatsbApplication.Current.CurrentUser.Id)
                    continue;

                PrivacyPermissionLevel locationPrivacy = GatsbApplication.Current.CurrentUser.LocationPrivacy;

                // Send to anyone in this venue that has permissions to see this.
                if (locationPrivacy == PrivacyPermissionLevel.Public ||
                    (locationPrivacy == PrivacyPermissionLevel.Friends && nearbyUser.FriendsWithRequester))
                {
                    // Send the user a notification.
                    GatsbServiceApplication.Current.SendUserSms(nearbyUser,
                        EmailAddress.Empty,
                        body);
                }
            }
        }
	}
}
